﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Device.Location;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Maps.Controls;
using INdTFramework.Maps;
using Microsoft.Phone.Maps.Services;

namespace MapSample
{
    public partial class MapWithRoute : PhoneApplicationPage
    {
        // Object to assist in manipulating the map control.
        HelperMap helperMap;

        // List of coordinates
        List<GeoCoordinate> listCoordinates;

        public MapWithRoute()
        {
            InitializeComponent();

            // Creation of an instance of the class HelperMap with the map control that is inserted into the view.
            helperMap = new HelperMap(MyMap);
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            MyMap.Visibility = Visibility.Collapsed;
            Loading.Visibility = Visibility.Visible;
            
            listCoordinates = new List<GeoCoordinate>();

            // Addition of the coordinates that will be used to calculate route between two geographical points.
            listCoordinates.Add(new GeoCoordinate(-3.09154981215481, -60.0121984981601));
            listCoordinates.Add(new GeoCoordinate(-3.12854319667214, -60.0122503256528));

            // Request for route calculation between the coordinates that are in 'listCoordinates'.
            // The ShowRouteOnMap function is required to execute some action with the resulting route.
            // The travel mode and route optimization are optional parameters.
            helperMap.TraceRoute(listCoordinates, ShowRouteOnMap, TravelMode.Walking, RouteOptimization.MinimizeTime);
        }


        void ShowRouteOnMap(Route route)
        {
            // Addition the route on the map control.
            // The object 'route' contains information such as the length in meters of the route and estimated time of arrival.
            helperMap.AddRoute(route);

            // Image that represents the first marker.
            var image = new Image { Source = new BitmapImage(new Uri("/Assets/pin1.png", UriKind.Relative)) };

            // Addition of the first marker on the map control.
            helperMap.AddMarker(listCoordinates[0], image, new Point(0.5, 0.5));

            // Image that represents the second marker.
            var image2 = new Image { Source = new BitmapImage(new Uri("/Assets/pin2.png", UriKind.Relative)) };

            // Addition of the second marker on the map control.
            helperMap.AddMarker(listCoordinates[1], image2, new Point(0.5, 1));

            // Definition of the center of the map control from the midpoint between two coordinates.
            helperMap.SetCenter(listCoordinates[0], listCoordinates[1]);

            Loading.Visibility = Visibility.Collapsed;
            MyMap.Visibility = Visibility.Visible;
        }
    }
}